package com.platform.modules.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.modules.mall.dao.MallShopsToActivityDao;
import com.platform.modules.mall.entity.MallActivityEntity;
import com.platform.modules.mall.entity.MallShopsToActivityEntity;
import com.platform.modules.mall.service.IAssociTagDataChange;
import com.platform.modules.mall.service.MallAssociTagService;
import com.platform.modules.mall.service.MallShopsToActivityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("mallShopsToActivityServiceImpl")
public class MallShopsToActivityServiceImpl extends ServiceImpl<MallShopsToActivityDao, MallShopsToActivityEntity> implements MallShopsToActivityService {

    @Resource
    private MallAssociTagService associTagService;
    @Resource
    private IAssociTagDataChange shopsActivityTag;

    @Override
    public List<String> getActivityIdList(String shopsId) {
        return this.listObjs(new LambdaQueryWrapper<MallShopsToActivityEntity>()
                .select(MallShopsToActivityEntity::getActivityId)
                .eq(MallShopsToActivityEntity::getShopsId,shopsId), Object::toString);
    }

    @Override
    public boolean saveShopsToActivty(MallActivityEntity activityEntity, List<String> list) {

        // 删除店铺标签
        List<String> shopsIds = this.getShopsIdList(activityEntity.getId());
        associTagService.delateByOtherId(activityEntity.getId(),shopsIds.stream().filter(x->!list.contains(x)).toArray(String[]::new));

        this.lambdaUpdate().eq(MallShopsToActivityEntity::getActivityId,activityEntity.getId()).remove();

        List<MallShopsToActivityEntity> entitys = new ArrayList<>();
        for (String str : list){
            MallShopsToActivityEntity entity = new MallShopsToActivityEntity();
            entity.setActivityId(activityEntity.getId());
            entity.setShopsId(str);
            entitys.add(entity);

            if(!shopsIds.contains(str)){
                // 添加店铺标签
                shopsActivityTag.add(str,activityEntity);
            }

        }
        return this.saveBatch(entitys);
    }

    @Override
    public List<String> getShopsIdList(String activityId) {
        return this.listObjs(new LambdaQueryWrapper<MallShopsToActivityEntity>()
                .select(MallShopsToActivityEntity::getShopsId)
                .eq(MallShopsToActivityEntity::getActivityId,activityId), Object::toString);
    }
}
